APP=cve-ease
VERSION=0.1.1
RELEASE=1
DOCKER_IMAGE=$(APP)-$(VERSION)
CONTAINER_NAME=$(APP)-$(VERSION)
DESTDIR=
RPM_DIR=/tmp/cve-ease-rpmbuild
PYTHON_LIB=$(shell python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
LIB_DIR=$(PYTHON_LIB)/cve_ease

.PHONY: default help version setversion
default:
	@echo ""
	@echo "Makefile for CTyunOS $(APP)"
	@echo "Maintainer: youyifeng <youyf2@chinatelecom.cn>"
	@echo ""
	@echo "help            show help info(default)"
	@echo "status          show docker status"
	@echo "build           build docker image"
	@echo "attach          attach container"
	@echo "destroy         destroy container"
	@echo "daemon          start an container with daemon"
	@echo "docker-shell    start docker and enter bash"
	@echo "run-in-docker   run cve-ease service in docker without interactive"
	@echo "clean           clean release file"
	@echo "kill            kill cve-ease program"
	@echo "install         install cve-ease directly"
	@echo "uninstall       uninstall cve-ease directly"
	@echo "gensrpm         generate srpm"
	@echo "gentarspec      generate archive tar.bz2 and spec file"
	@echo "rpmbuild        build binary rpm, store in /tmp/cve-ease-rpmbuild"

help: default

version:
	@echo "VERSION:$(VERSION)"
	@echo "RELEASE:$(RELEASE)"
	@echo "PYTHON_LIB:$(PYTHON_LIB)"
	@echo "APP_LIB:$(LIB_DIR)"

setversion:
	@sed -i "/^VERSION = /c VERSION = '$(VERSION)'" cve-ease*.py
	@sed -i "/^RELEASE = /c RELEASE = '$(RELEASE)'" cve-ease*.py
	@echo "set version to $(VERSION)-$(RELEASE)"
	@echo "All done!"

.PHONY: run debug msgtest kill install uninstall
run:
	EASE_CONFIG=$(CONFIG_DIR)/cve-ease.cfg python3 cve-ease.py

debug:
	EASE_CONFIG=$(CONFIG_DIR)/cve-ease.cfg python3 cve-ease.py --debug

msgtest:
	EASE_CONFIG=$(CONFIG_DIR)/cve-ease.cfg python3 cve-ease.py wecom -t
	EASE_CONFIG=$(CONFIG_DIR)/cve-ease.cfg python3 cve-ease.py dingding -t
	EASE_CONFIG=$(CONFIG_DIR)/cve-ease.cfg python3 cve-ease.py feishu -t

kill:
	@pkill -x cve-ease || :
	@echo "kill done!"

install: kill setversion
	@echo "* install cve-ease..."
	mkdir -p ${DESTDIR}/usr/bin &> /dev/null || :
	mkdir -p ${DESTDIR}/etc/cve-ease &> /dev/null || :
	mkdir -p ${DESTDIR}/usr/lib/systemd/system &> /dev/null || :
	mkdir -p ${DESTDIR}/var/log/cve-ease &> /dev/null || :
	mkdir -p ${DESTDIR}/$(LIB_DIR) &> /dev/null || :
	mkdir -p ${DESTDIR}/usr/share/cve-ease &> /dev/null || :
	install -v -m 644  cve-ease.cfg ${DESTDIR}/etc/cve-ease
	install -v -m 755  cve-ease.py ${DESTDIR}/usr/bin/cve-ease
	install -v -m 644  cve-ease.service ${DESTDIR}/usr/lib/systemd/system/
	install -v -m 644  cve-ease.timer ${DESTDIR}/usr/lib/systemd/system/
	cd site-packages/cve_ease/ && find . -type f -exec install -Dm 644 "{}" "${DESTDIR}/$(LIB_DIR)/{}" \;
	@systemctl daemon-reload || :
	@echo "* Install cve-ease Success!"

uninstall:
	rm -rf ${DESTDIR}/etc/cve-ease
	rm -f ${DESTDIR}/usr/bin/cve-ease
	rm -f ${DESTDIR}/usr/lib/systemd/system/cve-ease.service
	rm -f ${DESTDIR}/usr/lib/systemd/system/cve-ease.timer
	rm -rf ${DESTDIR}/$(LIB_DIR)
	@systemctl daemon-reload || :
	@echo "* Uninstall cve-ease Success!"

.PHONY: build attach destroy docker-purge docker-clean docker-shell daemon run-in-docker docker-raw
build:
	@echo "* Build cve-ease ..."
	docker build -t $(DOCKER_IMAGE) .
	docker image ls $(DOCKER_IMAGE)
	@echo "* Docker build ok"

attach:
	docker exec -it $(CONTAINER_NAME) /bin/bash

destroy:
	docker container rm --force $(CONTAINER_NAME) || :

docker-purge:
	@echo "* Purge cve-ease container and image"
	docker container ls -a | grep cve-ease | awk '{print $$1}' | xargs docker container rm --force &> /dev/null || :
	docker image ls | grep cve-ease | awk '{print $$3}' | xargs docker image rm &> /dev/null || :

docker-clean:
	@echo "* Clean cve-ease container"
	docker container ls -a | grep cve-ease | awk '{print $$1}' | xargs docker container rm --force &> /dev/null || :
	@echo "* Clean done"

docker-shell: docker-clean build daemon
	@echo "* Enter cve-ease docker..."
	@docker exec -e PS1="[ cve-ease-docker@\w ]# " -it \
        $(CONTAINER_NAME) \
        /bin/bash

daemon:
	@echo "* Run cve-ease docker daemon..."
	@docker run -d --privileged \
	    -v `pwd`:/host-data \
        --name $(CONTAINER_NAME) \
        $(DOCKER_IMAGE) \
        /sbin/init
	@docker exec -e PS1="[ cve-ease-docker@\w ]# " -it \
        $(CONTAINER_NAME) \
        /bin/bash -c "cd /data/ && make install"

docker-raw: docker-clean build
	@echo "* Run cve-ease docker-raw..."
	@docker run -d --privileged \
	    -v `pwd`:/host-data \
        --name $(CONTAINER_NAME) \
        $(DOCKER_IMAGE) \
        /sbin/init
	@docker exec -e PS1="[ cve-ease-docker@\w ]# " -it \
        $(CONTAINER_NAME) \
        /bin/bash

run-in-docker: docker-clean build daemon
	@echo "* Enable cve-ease service..."
	@docker exec -e PS1="[ cve-ease-docker@\w ]# " -it \
        $(CONTAINER_NAME) \
        /bin/bash -c "systemctl enable --now cve-ease.timer"

.PHONY: status stop restart start watch
status:
	systemctl daemon-reload
	systemctl show cve-ease.timer --no-pager || :
	systemctl list-timers --all || :
	systemctl status --no-pager cve-ease.service cve-ease.timer -l || :

stop:
	systemctl daemon-reload || :
	systemctl stop cve-ease.service cve-ease.timer

restart:
	systemctl daemon-reload || :
	systemctl restart cve-ease.service cve-ease.timer

start:
	systemctl daemon-reload || :
	systemctl start cve-ease.service cve-ease.timer

watch:
	journalctl -xe -f -u cve-ease

.PHONY: gensrpm gentarspec rpmbuild clean
gensrpm: setversion
	[ -d $(RPM_DIR) ] && rm -rf $(RPM_DIR) || :
	mkdir -p $(RPM_DIR)/{SOURCES,SRPMS}
	tar --exclude cve-ease.cfg.decrypt --transform "s/^./cve-ease-$(VERSION)/" -jcvf $(RPM_DIR)/SOURCES/cve-ease-$(VERSION).tar.bz2 .
	cp -a cve-ease.spec ../
	@sed -i "s/VERSION/$(VERSION)/g" ../cve-ease.spec
	@sed -i "s/RELEASE/$(RELEASE)/g" ../cve-ease.spec
	@type rpmbuild
	rpmbuild -D "_topdir $(RPM_DIR)"  -bs ../cve-ease.spec
	@mv $(RPM_DIR)/SRPMS/*.src.rpm ../
	@ls -alh ../*.src.rpm
	@echo "gensrpm done!"

gentarspec: setversion
	cp -a cve-ease.spec ../
	@sed -i "s/VERSION/$(VERSION)/g" ../cve-ease.spec
	@sed -i "s/RELEASE/$(RELEASE)/g" ../cve-ease.spec
	tar --exclude cve-ease.cfg.decrypt --transform "s/^./cve-ease-$(VERSION)/" -jcvf ../cve-ease-$(VERSION).tar.bz2 .
	@ls -alh ../*.spec ../*.tar.bz2
	@echo "gentarspec done!"

rpmbuild: gensrpm
	rpmbuild  --define "_topdir  $(RPM_DIR)" -ba ../cve-ease.spec
	find /tmp/cve-ease-rpmbuild/
	@echo "rpmbuild done!"

clean:
	rm -f release/* || :
	rm -f ../$(APP)*.src.rpm || :
	rm -f ../$(APP)*.bz2 || :
	rm -f ../$(APP).spec || :
	[ -d $(RPM_DIR) ] && rm -rf $(RPM_DIR) || :
	@echo "clean done!"

